PSP2_SDK_DIR ?= /opt/sce/psp2/sdk

### target settings

TARGET  := psp2-app
OBJS    = main.o common.o heap.o event.o render.o
VP_OBJS = cube_v.gxp.o clear_v.gxp.o
FP_OBJS = cube_f.gxp.o clear_f.gxp.o
LIBS    = \
	-lScePerf_stub \
	-lSceNet_stub -lSceRtc_stub -lSceTouch_stub \
	-lSceDbg_stub -lSceGxm_stub -lSceDisplay_stub -lSceCtrl_stub -lSceDbgFont -lSceRazorCapture_stub_weak -lSceSysmodule_stub -lSceDbg_stub -lstdc++_stub -lm_stub
MINFO   = module_info.o
LIBGCC_AEABI = libgcc_aeabi.o

### browser core libraries
ifeq ($(BUILD_HOST), mingw)
LIBDIR = ../../lib
else
LIBDIR = ../builder/psp2-release/lib
endif

LIBDIR_LOCAL = ./lib
PLATFORM_LIBS   += \
	$(LIBDIR)/libwebkitgtk-1.0.a \
	$(LIBDIR)/libJavaScriptCore.a \
	$(LIBDIR)/libcairo.a \
	$(LIBDIR)/libcurl.a \
	$(LIBDIR)/libfreetype.a \
	$(LIBDIR)/libjpeg.a \
	$(LIBDIR)/libpixman-1.a \
	$(LIBDIR)/libpng.a \
	$(LIBDIR)/libsicui18n.a \
	$(LIBDIR)/libsicuuc.a \
	$(LIBDIR)/libsicudata.a \
	$(LIBDIR)/libsqlite3.a \
	$(LIBDIR)/libssl.a \
	$(LIBDIR)/libcrypto.a \
	$(LIBDIR)/libxml2.a \
	$(LIBDIR)/libz.a \
	$(LIBDIR)/libpsp2_compat.a

### build flags

CPPFLAGS = -D__PSP2__
CFLAGS =  \
	-O0 -mthumb \
	-I$(PSP2_SDK_DIR)/target/include_common \
	-I$(PSP2_SDK_DIR)/target/include

CXXFLAGS = $(CFLAGS) -fno-rtti -fno-exceptions -Wno-c++0x-compat

PRXFLAGS = 

### special flags

ifeq ($(USE_WEBKIT), yes)
PLATFORM_LIBS_LOCAL = $(subst $(LIBDIR),$(LIBDIR_LOCAL),$(PLATFORM_LIBS))
CPPFLAGS += -DUSE_WEBKIT
endif

ifeq ($(USE_NETCTL), yes)
CFLAGS += -Ivsh/sdk/target/include
CPPFLAGS += -DUSE_NETCTL
LIBS += vsh/sdk/target/lib/libSceNetCtl_stub_weak.a
endif

ifeq ($(NETTEST), yes)
OBJS     += nettest.o
CPPFLAGS += -DNETTEST
CFLAGS   += -I../builder/psp2-release/include/psp2_compat
CFLAGS   += -I$(PSP2_SDK_DIR)/target/
PLATFORM_LIBS_LOCAL = $(LIBDIR_LOCAL)/libpsp2_compat.a
endif

### build tools

ifeq ($(BUILD_HOST), mingw)
  # normalize path PSP2_SDK_DIR to /c/foo/bar/baz style
  PSP2_SDK_DIR	:= $(subst \,/,$(SCE_PSP2_SDK_DIR))
  PSP2_SDK_DIR	:= $(shell echo $(PSP2_SDK_DIR) | sed 's;^\(.\):;/\1/;')

  BUILD_TOOL_PREFIX		= $(PSP2_SDK_DIR)/host_tools/build/bin/

  CC         = sh ../builder/psp2snc.sh
  CXX        = sh ../builder/psp2snc.sh
  RANLIB     = $(BUILD_TOOL_PREFIX)psp2snarl s
  CGC        = $(BUILD_TOOL_PREFIX)psp2cgc
  OBJCOPY    = $(BUILD_TOOL_PREFIX)psp2bin
  CP         = cp
  LD         = $(BUILD_TOOL_PREFIX)psp2ld
  MAKE_FSELF = python.exe $(PSP2_SDK_DIR)\..\internal\bin\win32\make_self_internal.py
else
  CC         = $(PSP2_SDK_DIR)/target/bin/psp2-gcc
  RANLIB     = $(PSP2_SDK_DIR)/target/bin/psp2-ranlib
  CGC        = ./tool/psp2cgc
  OBJCOPY    = $(PSP2_SDK_DIR)/target/bin/psp2-objcopy
  CP         = cp
  LD         = $(PSP2_SDK_DIR)/target/bin/psp2-ld
  MAKE_FSELF = $(PSP2_SDK_DIR)/target/bin/linux/make_fself
endif

### make rules

all: $(TARGET).self

$(TARGET).self: $(TARGET).elf
	@echo [ Make SELF ] $@
ifeq ($(BUILD_HOST), mingw)
	$(MAKE_FSELF) -input $(CURDIR)/$< -output $(CURDIR)/$@ -index uprx_game_vs0
else
	$(MAKE_FSELF) $< $@
endif
	$(CP) $@ ~/mnt/svn/trunk/vsh/dist/$@

$(TARGET).elf: $(OBJS) $(VP_OBJS) $(FP_OBJS) $(MINFO) $(PLATFORM_LIBS_LOCAL) $(LIBGCC_AEABI)
	@echo [ Link ] $@
	$(CC) $(PRXFLAGS) $(MINFO) $(OBJS) $(VP_OBJS) $(FP_OBJS) $(PLATFORM_LIBS_LOCAL) $(LIBS) $(LIBGCC_AEABI) -o $@

$(VP_OBJS): %.gxp.o: %.cg
	@echo [ Compile ] $<
	$(CGC) --cache --profile sce_vp_psp2 $< -o $*.gxp
ifeq ($(BUILD_HOST), mingw)
	$(OBJCOPY) $*.gxp -b2e -o $@
else
	$(OBJCOPY) -I binary -O elf32-littlearm -B arm $*.gxp $@
endif

$(FP_OBJS): %.gxp.o: %.cg
	@echo [ Compile ] $<
	@$(CGC) --cache --profile sce_fp_psp2 $< -o $*.gxp
ifeq ($(BUILD_HOST), mingw)
	$(OBJCOPY) $*.gxp -b2e -o $@
else
	$(OBJCOPY) -I binary -O elf32-littlearm -B arm $*.gxp $@
endif

%.o: %.emd
	@echo [ Compile module info ] $@
ifeq ($(BUILD_HOST), mingw)
	$(CC) --prx $(EMDFLAGS) $(CFLAGS) $(TARGET_MACH) -c $< -o $@
else
	$(CC) -zgenentry $(EMDFLAGS) $(CPPFLAGS) $(TARGET_MACH) -c $< -o $@
endif

%.o: %.cpp
	@echo [ Compile ] $<
	$(CC) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@

%.o: %.c
	@echo [ Compile ] $<
	$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@

$(LIBDIR_LOCAL)/%:$(LIBDIR)/%
	@echo [ Copy ] $@
	@$(CP) $< $@
#	@$(RANLIB) $@

clean:
	@echo [ Clean ] $(TARGET)
	@$(RM) *.o *.gxp *.sdb *.map *.lst *.dis *.elf *.a *.self
	@$(RM) *.bak *.tmp
	@$(RM) $(MINFO)

clean_lib:
	@$(RM) $(LIBDIR_LOCAL)/*
